package com.josework.springhelloworld.entity.pythagorean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PythagoreanNum {
    private int a;
    private int b;
    private int c;

    public static void main(String[] args) throws IOException {
        boolean pythagorean = isPythagorean(3, 4);
        int[] array = {3,4,5,6};

        Stream<int[]> pythagoreanTriples =
                IntStream.rangeClosed(1, 100)
                        .boxed()
                        .flatMap(a ->
                                IntStream.rangeClosed(a, 100)
                                        .filter(b -> Math.sqrt(a*a + b*b) % 1 == 0)
                                        .mapToObj(b ->
                                                new int[]{a, b, (int)Math.sqrt(a * a + b * b)})
                        );
        pythagoreanTriples.limit(5)
                .forEach(t -> System.out.println(t[0] + ", " + t[1] + ", " + t[2]));

        Stream<double[]> pythagoreanTriples2 = IntStream.rangeClosed(1, 100)
                .boxed()
                .flatMap(a -> IntStream.rangeClosed(a, 100)
                        .mapToObj(b -> new double[]{a, b, Math.sqrt(a * a + b * b)})
                        .filter(t -> t[2] % 1 == 0)
                );
        pythagoreanTriples2.limit(5)
                .forEach( t-> System.out.println(t[0] + ", " + t[1] + ", " + t[2]));

        //由值创建流
        Stream<String> stream = Stream.of("Java 8", "Lambda", "In", "Action");
        stream.map(String::toUpperCase).forEach(System.out::println);
        Stream<String> emptyStream = Stream.empty();

        //由数组创建流
        int[] numbers = {2,3,5,6,78,33,12};
        int sum = Arrays.stream(numbers).sum();

        //由文件生成流
        Stream<String> lines =
                Files.lines(Paths.get("src/main/resources/data.txt"), Charset.defaultCharset());
        long count = lines.flatMap(line -> Arrays.stream(line.split("")))
                .distinct()
                .count();

        boolean pythagorean1 = isPythagorean(3, 6);

        Stream.iterate(new int[]{0,1},
                    t -> new int[]{t[1],t[1]+t[0]}
                )
                .limit(20)
                .forEach(t -> System.out.println("(" + t[0] + "," + t[1] +")"));




    }

    private static boolean isPythagorean(int a, int b){
        //使用expr % 1表示。如果它不是整数，那就是说c不是整数
        return Math.sqrt(a * a + b * b) % 1 == 0;
    }



}
